package dao.impl;

import dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pogo.User;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoimpl implements UserDao {

    @Override
    public List<User> findAll() {
        String resource = "mybatis-config.xml";
        SqlSession sqlsession = null;
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlsession = sqlSessionFactory.openSession();
            List<User> user = sqlsession.getMapper(UserDao.class).findAll();
            return user;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
           sqlsession.close();
        }
    }
}
